package Cain.pcxbook  
{
	import Cain.MkMc.Bk;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLRequest;
	import com.greensock.TweenLite;
	import flash.utils.getDefinitionByName;
	/**
	 * 阿Cain牌全自動loader
	 * @author Cain 2010
	 */
	public class CainLoader extends Sprite
	{
		/**
		 * Loader物件
		 */
		public var loader:Loader;
		private var bg:Bk;
		private var pcx:Sprite;
		private var loader_loaded:int; //載入旗標
		private var loadingMovieClassName:String = "";
		/**
		 * 自動化Loader
		 * @param	_ww 寬
		 * @param	_hh 高
		 * @param	_bgcolor 底色
		 * @param	_alpha 底色透明度
		 */
		public function CainLoader(_ww:Number = 100,_hh:Number = 100,_bgcolor:uint = 0,_alpha:Number = 0) 
		{
			loader = new Loader();
			bg = new Bk(_ww, _hh, _bgcolor, _alpha);
			addChild(bg);
			setup();
		}
		/**
		 * 載入圖
		 * @param	_url 網址
		 */
		public function load(_url:String):void 
		{
			if (loadingMovieClassName!="") 
			{//如果loadingClassName有東西
				try 
				{
					var new_class:Class = getDefinitionByName(loadingMovieClassName) as Class;
					var kk:DisplayObject = new new_class();
					kk.x = bg.width >> 1;
					kk.y = bg.height >> 1;
					kk.name = "loadingMC";
					addChild(kk);
				}catch (e:Error)
				{
					throw(new Error("元件庫中沒有" + loadingMovieClassName));
				}
				
			}
			pcx = new Sprite();
			pcx.x = bg.width >> 1;
			pcx.y = bg.height >> 1;
			pcx.addChild(loader);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_onCOMPLETE);
			try 
			{
				loader.load(new URLRequest(_url));
			}catch (err:Error)
			{
				throw(new Error("載入" + _url + "錯誤"));
			}
			
			loader_loaded = 1; //設定旗標為載入中
		}
		/**
		 * 載入旗標 0未載入 1載入中 2已載入
		 */
		public function get loaded():int 
		{
			return loader_loaded;
		}/**
		 * 設定載入動畫
		 */
		public function set loadMovie(_className:String):void 
		{
			if (loaded==0) 
			{
				loadingMovieClassName = _className;
			}else 
			{
				throw(new Error("設定載入動畫,須在load前.."));
			}
		}
		private function loader_onCOMPLETE(e:Event):void 
		{
			if (loadingMovieClassName!="") 
			{
				removeChild(getChildByName("loadingMC"));
			}
			loader_loaded = 2; //設定旗標為載完
			loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loader_onCOMPLETE);
			
			loader_resize();//設定大小使其符合
			set_mk();
			loader.x = -(loader.width >> 1);
			loader.y = -(loader.height >> 1);
			addChild(pcx);
			TweenLite.from(pcx, .5, { alpha:0 } );
		}
		
		private function set_mk():void
		{
			var _mk:Bk = new Bk(bg.width, bg.height);
			_mk.x = -(bg.width >> 1);
			_mk.y = -(bg.height >> 1);
			pcx.addChild(_mk);
			pcx.mask = _mk;
		}
		/**
		 * 設定大小使其符合
		 */
		private function loader_resize():void
		{
			var _w_sc:Number = bg.width / loader.width;
			var _h_sc:Number = bg.height / loader.height;
			if (_w_sc>_h_sc) 
			{
				loader.scaleX = _w_sc;
				loader.scaleY = _w_sc;
			}else 
			{
				loader.scaleX = _h_sc;
				loader.scaleY = _h_sc;
			}
		}
		private function setup():void
		{
			
			addEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			addEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
		}
		
		private function bg_setup():void
		{
			
		}
		private function onADDED_TO_STAGE(e:Event):void 
		{
			init();
			addEventListeners();
		}
		private function init():void
		{
			
		}
		private function addEventListeners():void
		{
			
		}
		private function removeEventListeners():void
		{
			if (loaded!=0) 
			{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loader_onCOMPLETE);
				//loader.unload();
			}
		}
		private function onREMOVED_FROM_STAGE(e:Event):void 
		{
			removeEventListeners();
			removeEventListener(Event.ADDED_TO_STAGE, onADDED_TO_STAGE);
			removeEventListener(Event.REMOVED_FROM_STAGE, onREMOVED_FROM_STAGE);
			loader = null;
		}
	}

}